package com.junxonline.instant.admin.service.config.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.junxonline.instant.dao.entity.model.config.Setting;
import com.junxonline.instant.dao.entity.vo.config.SettingVO;
import com.junxonline.instant.dao.mapper.config.SettingMapper;
import com.junxonline.instant.admin.service.config.SettingService;
import com.junxonline.instant.common.constant.SQLConstant;
import com.junxonline.instant.common.entity.common.CommonReqPage;
import com.junxonline.instant.common.entity.common.CommonRespPage;
import com.junxonline.instant.common.enumeration.AdminErrorEnum;
import com.junxonline.instant.common.exception.AdminBizException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 参数管理接口实现类
 *
 * @author JunX
 * @date 2021-08-12
 */
@Service
@Transactional
public class SettingServiceImpl extends ServiceImpl<SettingMapper, Setting> implements SettingService {

    @Autowired
    private SettingMapper settingMapper;

    /**
     * 根据系统参数名称查询参数值
     *
     * @param vo 请求参数
     * @return Setting
     */
    @Override
    public Setting querySettingByName(SettingVO vo) {
        return settingMapper.selectOne(new LambdaQueryWrapper<Setting>().eq(Setting::getName, vo.getName()).last(SQLConstant.LIMIT_1));
    }

    /**
     * 查询参数列表
     *
     * @param vo 请求参数
     * @return CommonRespPage<Setting>
     */
    @Override
    public CommonRespPage<Setting> queryList(CommonReqPage<SettingVO> vo) {
        LambdaQueryWrapper<Setting> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(vo.getQueryParams().getName()), Setting::getName, vo.getQueryParams().getName());
        return CommonRespPage.success(settingMapper.selectPage(new Page<>(vo.getPage(), vo.getPageSize()), wrapper));
    }

    /**
     * 查询参数
     *
     * @param id ID
     * @return Setting
     */
    @Override
    public Setting query(Long id) {
        Setting setting = settingMapper.selectById(id);
        Assert.notNull(setting, AdminErrorEnum.DATA_IS_NOT_EXISTS.getMsg());
        return setting;
    }

    /**
     * 新增参数
     *
     * @param setting 请求参数
     */
    @Override
    public void add(Setting setting) {
        Integer count = settingMapper.selectCount(new LambdaQueryWrapper<Setting>()
                .eq(Setting::getName, setting.getName()));
        if (count > 0) {
            throw new AdminBizException(AdminErrorEnum.DATA_IS_EXISTS);
        }
        settingMapper.insert(setting);
    }

    /**
     * 编辑参数
     *
     * @param setting 请求参数
     */
    @Override
    public void edit(Setting setting) {
        Integer count = settingMapper.selectCount(new LambdaQueryWrapper<Setting>()
                .ne(Setting::getId, setting.getId())
                .eq(Setting::getName, setting.getName()));
        if (count > 0) {
            throw new AdminBizException(AdminErrorEnum.DATA_IS_EXISTS);
        }
        settingMapper.updateById(setting);
    }

    /**
     * 删除参数
     *
     * @param ids 请求参数
     */
    @Override
    public void delete(List<Long> ids) {
        settingMapper.deleteBatchIds(ids);
    }

}
